#include "main.h"

#include <CommDlg.h>

#define OPT_UPDATEINTERVAL			"UpdateInterval"
#define OPT_UPDATETIMEOUT			"UpdateTimeout"
#define OPT_SEARCHTIMEOUT			"SearchTimeout"

Options options;

int gameIndex(char* game) {
	for (int index = 0; index < sizeof(gameList) / sizeof(Game); index++) {
		if (strcmp(game, gameList[index].name) == 0)
			return index;
	}
	return -1;
}

BOOL CALLBACK OptionsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);

int InitOptions(WPARAM wParam, LPARAM lParam) {
	OPTIONSDIALOGPAGE odp = {0};
	odp.cbSize = sizeof(odp);
	odp.position = 910000000;
	odp.hInstance = g_hInstance;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPTIONS);
	odp.pszTitle = LPGEN(PROTONAME);
	odp.pszGroup = LPGEN("Plugins");
	odp.pszTab = LPGEN("Common");
	odp.pfnDlgProc = OptionsDlgProc;
	odp.flags = ODPF_BOLDGROUPS;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);
	return 0;
}


void LoadDefaults() {
	options.dwUpdateInterval = 60;
	options.dwUpdateTimeout = 100;
	options.dwSearchTimeout = 1000;
	for (int i = 0; i < sizeof(gameList) / sizeof(Game); i++) {
		DBWriteContactSettingString(0, PROTONAME, gameList[i].name, gameList[i].path);
	}
	DBWriteContactSettingString(0, PROTONAME, "Options" , "ready");
}


void LoadOptions() {
	DBVARIANT dbv;
	DBGetContactSetting(0, PROTONAME, "Options", &dbv);
	if (!dbv.pszVal || (strcmp(dbv.pszVal, "ready") != 0)) {
		return LoadDefaults();
	}
	GSSetOptInt(GS_OPTION_COLORFILTER, GS_TRUE);
	options.dwUpdateInterval = DBGetContactSettingWord(0, PROTONAME, OPT_UPDATEINTERVAL, 0);
	options.dwUpdateTimeout = DBGetContactSettingWord(0, PROTONAME, OPT_UPDATETIMEOUT, 0);
	options.dwSearchTimeout = DBGetContactSettingWord(0, PROTONAME, OPT_SEARCHTIMEOUT, 0);
	GSSetOptInt(GS_OPTION_TIMEOUT, options.dwUpdateTimeout);

	for (int i = 0; i < sizeof(gameList) / sizeof(Game); i++) {
		DBGetContactSetting(0, PROTONAME, gameList[i].name, &dbv);
		strcpy(gameList[i].path, dbv.pszVal);
	}
	DBFreeVariant(&dbv);
}


void SaveOptions() {
	DBWriteContactSettingWord(0, PROTONAME, OPT_UPDATEINTERVAL, options.dwUpdateInterval);
	DBWriteContactSettingWord(0, PROTONAME, OPT_UPDATETIMEOUT, options.dwUpdateTimeout);
	DBWriteContactSettingWord(0, PROTONAME, OPT_SEARCHTIMEOUT, options.dwSearchTimeout);
	for (int i = 0; i < sizeof(gameList) / sizeof(Game); i++) {
		DBWriteContactSettingString(0, PROTONAME, gameList[i].name, gameList[i].path);
	}
}


BOOL CALLBACK OptionsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
	HWND hwndGame;
	HWND hwndPath;
	switch (msg) {
	case WM_INITDIALOG: {
		TranslateDialogDefault(hwndDlg);
		SetDlgItemInt(hwndDlg, IDC_UPDATE_INTERVAL, options.dwUpdateInterval, TRUE);
		SetDlgItemInt(hwndDlg, IDC_UPDATE_TIMEOUT, options.dwUpdateTimeout, TRUE);
		SetDlgItemInt(hwndDlg, IDC_SEARCH_TIMEOUT, options.dwSearchTimeout, TRUE);
		for (int i = 0; i < sizeof(gameList) / sizeof(Game); i++) {
			SendMessage(GetDlgItem(hwndDlg, IDC_GAMES), CB_ADDSTRING, 0, (LPARAM)gameList[i].name);
		}
		SendMessage(GetDlgItem(hwndDlg, IDC_GAMES), WM_KEYDOWN, (WPARAM)VK_DOWN, 0);
		break;
	}
	case WM_COMMAND: {
		if (HIWORD(wParam) == EN_CHANGE && (HWND)lParam == GetFocus()) {
			switch (LOWORD(wParam)) {
				case IDC_PATH:
				case IDC_UPDATE_INTERVAL:
				case IDC_UPDATE_TIMEOUT:
				case IDC_SEARCH_TIMEOUT:
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
			}
		}
		if (HIWORD(wParam) == CBN_SELCHANGE && LOWORD(wParam) == IDC_GAMES) {
			hwndGame = GetDlgItem(hwndDlg, IDC_GAMES);
			char szGame[MAX_GAME];
			GetWindowTextA(hwndGame, szGame, sizeof(szGame));
			int index = gameIndex(szGame);
			if (index == -1)
				break;
			hwndPath = GetDlgItem(hwndDlg, IDC_PATH);
			SendMessageA(hwndPath, WM_SETTEXT, TRUE, (LPARAM)gameList[index].path);
			break;
		}
		if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BROWSE) {
			hwndPath = GetDlgItem(hwndDlg, IDC_PATH);
			char szFile[MAX_PATH];
			GetWindowTextA(hwndPath, szFile, sizeof(szFile));
			OPENFILENAMEA ofn = { 0 };
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = hwndDlg;
			ofn.nMaxFile = sizeof(szFile);
			ofn.lpstrFile = szFile;
			ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
			if (GetOpenFileNameA(&ofn) != TRUE) {
				break;
			}
			SendMessageA(hwndPath, WM_SETTEXT, TRUE, LPARAM(szFile));
			hwndGame = GetDlgItem(hwndDlg, IDC_GAMES);
			char szGame[MAX_GAME];
			GetWindowTextA(hwndGame, szGame, sizeof(szGame));
			strcpy(gameList[gameIndex(szGame)].path, szFile);
			SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
			break;
		}
	}
	case WM_NOTIFY: {
		NMHDR* nmhdr = (NMHDR*)lParam;
		if ((int)wParam == 0) {
			if (nmhdr->code == PSN_APPLY) {
				options.dwUpdateInterval = GetDlgItemInt(hwndDlg, IDC_UPDATE_INTERVAL, NULL, TRUE);
				options.dwUpdateTimeout = GetDlgItemInt(hwndDlg, IDC_UPDATE_TIMEOUT, NULL, TRUE);
				options.dwSearchTimeout = GetDlgItemInt(hwndDlg, IDC_SEARCH_TIMEOUT, NULL, TRUE);
				SaveOptions();
				return TRUE;
			}
		}
		break;
	}
	} // switch (msg)
	return FALSE;
}
